//
//  SmallCardProductCell.swift
//  产品列表 cell
//
//  Created by 千策万划-mini on 2025/5/8.
//

import Foundation

class SmallCardProductCell: UITableViewCell {
    
    var blockApplyClick: ((SmallCardProductCell) -> Void)?
    
    // MARK: - lazy
    lazy var imageBack: UIButton = {
        let result = UIButton()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(345), W(133))
        result.setBackgroundImage(UIImage(named: "icon_small_card_cell_bg"), for: .normal)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var imageLogo: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(40), W(40))
        GlobalMethod.setRoundView(view: result, color: .clear, numRound: 13, width: 0)
        return result
    }()
    lazy var labelAmountUnit: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var labelAmount: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var imageDayBg: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "icon_small_card_cell_day_bg")
        return result
    }()
    lazy var labelDay: UILabel = {
        let result = UILabel()
        result.textColor = UIColor(hex: "#5F2F0D")
        result.font = UIFont.customMediumFont(size: 14)
        return result
    }()
    lazy var labelName: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var btnApply: UIButton = {
        let result = UIButton()
        result.setBackgroundImage(UIImage(named: "icon_order_nodata_apply"), for: .normal)
        result.widthHeight = XY(W(82), W(34))
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        
        let label = OutlineLabel()
        label.resetView(
            title: "Apply",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 14),
            shadowColor: UIColor(hex: "#004600"),
            outlineColor: UIColor(hex: "#1B4506")
        )
        label.centerXCenterY = XY(result.width/2, result.height/2)
        result.addSubview(label)
        
        return result
    }()
    
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = .clear
        self.backgroundColor = .clear
        self.selectionStyle = .none
        setUpView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpView() {
        contentView.addSubview(imageBack)
        imageBack.addSubview(imageLogo)
        imageBack.addSubview(labelAmountUnit)
        imageBack.addSubview(labelAmount)
        imageBack.addSubview(imageDayBg)
        imageBack.addSubview(labelDay)
        imageBack.addSubview(labelName)
        imageBack.addSubview(btnApply)
    }
    
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            if let block = blockApplyClick {
                block(self)
            }
            break
        default:
            break
        }
    }
    
    // MARK: - 重置界面
    @objc func resetCellWithModel(_ result: Any){
        guard let model = result as? ModelHomeDifferenceImpulsive else { return }
        
        imageBack.centerXTop = XY(SCREEN_WIDTH/2, 0)
        
        imageLogo.leftTop = XY(W(15), W(19))
        imageLogo.setImage(urlStr: model.lucky)
        
        labelAmountUnit.resetView(
            title: "₱",
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 20),
            shadowColor: UIColor(hex: "#5F2F0D"),
            outlineColor: UIColor(hex: "#5F2F0D")
        )
        labelAmountUnit.left = imageLogo.right+8
        
        labelAmount.resetView(
            title: UnPackStr(model.sounded),
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 26),
            shadowColor: UIColor(hex: "#5F2F0D"),
            outlineColor: UIColor(hex: "#5F2F0D")
        )
        labelAmount.leftCenterY = XY(labelAmountUnit.right, imageLogo.centerY)
        labelAmountUnit.bottom = labelAmount.bottom-W(3)
        
        labelDay.fitTitle("\(model.relieved) ｜\(model.dead)/day", variable: 0)
        imageDayBg.widthHeight = XY(labelDay.width+8*2, W(34))
        imageDayBg.rightCenterY = XY(imageBack.width-imageLogo.left, imageLogo.centerY)
        labelDay.centerXCenterY = XY(imageDayBg.centerX, imageDayBg.centerY)
        
        labelName.resetView(
            title: UnPackStr(model.sooner),
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 20),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        
        btnApply.rightBottom = XY(imageBack.width-imageLogo.left, imageBack.height-W(14))
        
        labelName.leftCenterY = XY(imageLogo.left, btnApply.centerY)
        
        height = imageBack.bottom+W(16)
    }
    
}
